package com.leetcode.no257;

import java.util.*;


class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode(int x) {
        val = x;
    }
}

public class Solution {
    List<String> res = new ArrayList<>();

    public List<String> binaryTreePaths(TreeNode root) {
        if (root == null) {
            return res;
        }
        String track = "";

        dfs(root, track);
        return res;
    }

    public void dfs(TreeNode root, String track) {
        if (root == null) {
            return;
        }

        track += String.valueOf(root.val);

        if (isLeaf(root)) {
            res.add(track);
        }

        track += "->";
        if (root.left != null) {
            dfs(root.left, track);
        }
        if (root.right != null) {
            dfs(root.right, track);
        }
    }

    public boolean isLeaf(TreeNode root) {
        // 当节点的左右子树都为空时是叶子节点
        if (root.left == null && root.right == null) {
            return true;
        }
        return false;
    }

}
